<template>
	<view class="changepass">
		<van-form @submit="onSubmit">
			<van-cell-group inset>
				<van-field v-model="oldpass" type="password" name="oldpass" label="旧密码" placeholder="旧密码"
					:rules="[{ required: true, message: '请填写旧密码' }]" />
				<van-field v-model="newpass" type="password" name="newpass" label="新密码" placeholder="新密码"
					:rules="[{ required: true, message: '请填写新密码' }]" />
			</van-cell-group>
			<div style="margin: 16px;">
				<van-button  block class="btn" type="primary" native-type="submit">
					确认
				</van-button>
			</div>
		</van-form>
	</view>
</template>

<script setup>
	import { showToast } from 'vant';
import {ref} from 'vue';
	import api from '../../lib/request.js'
	const oldpass = ref('');
	const newpass = ref('');
	
	const onSubmit = (values) => {
		let {oldpass,newpass:password } = values
		console.log(oldpass ,password);
		if(oldpass == password){
			showToast('新密码与旧密码不能相同')
			newpass.value = ''
			return
		}
		
		api.post(`/user/user/update?password=${password}`).then(res=>{
			if(res.code){
				showToast('密码修改成功,请重新登录')
				setTimeout(()=>{
					uni.reLaunch({
						url:'/views/login/login'
					})
				},1500)
			}
			console.log(res)
		})
	};
</script>

<style lang="scss" scoped>
	.changepass{
		width: 100%;
		height: calc(100vh - 44px);
	}
	.btn{
		position: fixed;
		bottom:0;
		left:0;
		font-size:18px;
	}

</style>